#include "StdAfx.h"

void ToLower(std::string& szInput)
{
	for(ui32 i = 0; i < szInput.size(); ++i)
	{
		if(::isalnum(szInput[i]))
			szInput[i] = tolower(szInput[i]);
	}
}

TextureUI::TextureUI()
{
	m_pCurQuad = 0;
	m_brushType = 0;
	m_curPos = 0;
	m_dialog = new Dialog(200, 200, 12, 11, "Texturing");
	m_dialog->m_visible = true;
	m_pCircle = m_dialog->AddRadioButton(20, 125, this, &TextureUI::BruhshSelProc);
	m_pCircle->SetText("Circle");
	m_pCircle->Check();
	m_pSquare = m_dialog->AddRadioButton(100, 125, this, &TextureUI::BruhshSelProc);
	m_pSquare->SetText("Square");
	m_tText = m_dialog->AddText(20, 150, "Curtex: (none)", 44);
	pL1 = m_dialog->AddCheckbox(this, 20, 170, "Layer 1", &TextureUI::SetTextureFlags);
	pL1->pData = (void*)1;
	pL2 = m_dialog->AddCheckbox(this, 150, 170, "Layer 2", &TextureUI::SetTextureFlags);
	pL2->pData = (void*)2;
	pL3 = m_dialog->AddCheckbox(this, 20, 195, "Layer 3", &TextureUI::SetTextureFlags);
	pL3->pData = (void*)4;
	pL4 = m_dialog->AddCheckbox(this, 150, 195, "Layer 4", &TextureUI::SetTextureFlags);
	pL4->pData = (void*)8;
	pFreeLayer = m_dialog->AddCheckbox(this, 20, 220, "Free layer", &TextureUI::SetTextureFlags, 53);
	pFreeLayer->pData = (void*)16;
	//SetTextureFlags(pFreeLayer, true);
	pFreeLayer->SetChecked(true);
	sSelection.SetTexFlags(16);
	
	//m_pTexEnabled = m_dialog->AddCheckbox(this, 20, 25, "Texturing active", &TextureUI::EnabledTexClick, 52);
	m_iText = m_dialog->AddText(20, 270, "Intensity: 10.00", 46);
	Scrollbar<TextureUI>* b = m_dialog->AddScrollbar(150, 270, 100, this, &TextureUI::IntensityProc);
	b->SetRange(10, 200);
	b->SetValue(100);
	m_gText = m_dialog->AddText(20, 295, "Strength: 1.0", 47);
	b = m_dialog->AddScrollbar(150, 295, 100, this, &TextureUI::GradientProc);
	b->SetRange(10, 100);
	b->SetValue(100);
	sUI.AddElement(m_dialog);
	m_intensity = 10.0f;
	m_gradient = 1.0f;
	m_curTexture = 0;
	Vertex2F coords[4];
	coords[0].y = 0.0f;
	coords[0].x = 28.0f / 32.0f;
	coords[1].y = 28.0f / 32.0f;
	coords[1].x = 28.0f / 32.0f;
	coords[2].y = 28.0f / 32.0f;
	coords[2].x = 0.0f;
	coords[3].y = 0.0f;
	coords[3].x = 0.0f;
	m_pNextT = m_dialog->AddButton(this, 20, 30, "", &TextureUI::NexTexProc, 100);
	m_pNextT->SetTextures("Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Up.blp", "Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Down.blp",
		"Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Disabled.blp");
	m_pNextT->SetCoordinates(coords);
	m_pNextT->m_enabled = false;
	coords[0].y = 28.0f / 32.0f;
	coords[0].x = 0.0f;
	coords[1].y = 0.0f;
	coords[1].x = 0.0f;
	coords[2].y = 0.0f;
	coords[2].x = 28.0f / 32.0f;
	coords[3].y = 28.0f / 32.0f;
	coords[3].x = 28.0f / 32.0f;
	m_pPrevT = m_dialog->AddButton(this, 330, 30, "", &TextureUI::PrevTexProc, 100);
	m_pPrevT->SetTextures("Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Up.blp", "Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Down.blp",
		"Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Disabled.blp");
	m_pPrevT->SetCoordinates(coords);
	m_pPrevT->m_enabled = false;
}

void TextureUI::NexTexProc(UIButton<TextureUI>*)
{
	if(m_curPos > 0)
		--m_curPos;
	else
		return;
	if(m_curPos == 0)
		m_pNextT->m_enabled = false;
	m_pPrevT->m_enabled = true;
	for(ui32 i = 0; i < m_favourites.size(); ++i)
	{
		m_favourites[i]->visible = (i - m_curPos) < 9 && i >= m_curPos;
		if((i - m_curPos) < 9 && i >= m_curPos)
			m_dialog->SetQuadPos(m_favourites[i], 20 + ((i - m_curPos) * 38), 60);
	}
}

void TextureUI::PrevTexProc(UIButton<TextureUI>*)
{
	if(m_curPos + 9 >= m_favourites.size())
		return;
	else
		++m_curPos;
	if(m_curPos + 9 == m_favourites.size())
		m_pPrevT->m_enabled = false;
	m_pNextT->m_enabled = true;
	for(ui32 i = 0; i < m_favourites.size(); ++i)
	{
		m_favourites[i]->visible = (i - m_curPos) < 9 && i >= m_curPos;
		if((i - m_curPos) < 9 && i >= m_curPos)
			m_dialog->SetQuadPos(m_favourites[i], 20 + ((i - m_curPos) * 38), 60);
	}
}

static std::map<std::string, std::vector<std::string>> mTextures;

void TextureUI::LoadButtonProc(UIButton<TextureUI>*)
{
}

void TextureUI::AddFavourite(std::string str)
{
	for(ui32 i = 0; i < m_favourites.size(); ++i)
		if(m_favourites[i]->szTexture == str)
			return;

	TextureQuad<TextureUI>* q = m_dialog->AddTexQuad(this, &TextureUI::TexQuadClick, str, Vertex2F(20, 30));
	m_favourites.push_front(q);
	if(m_curPos + 9 < m_favourites.size())
		m_pPrevT->m_enabled = true;
	for(ui32 i = 0; i < m_favourites.size(); ++i)
	{
		m_favourites[i]->visible = (i - m_curPos) < 9 && i >= m_curPos;
		if((i - m_curPos) < 9 && i >= m_curPos)
			m_dialog->SetQuadPos(m_favourites[i], 20 + ((i - m_curPos) * 38), 60);
	}
}

void TextureUI::TexQuadClick(TextureQuad<TextureUI>* pQuad)
{
	for(ui32 i = 0; i < m_favourites.size(); ++i)
		m_favourites[i]->border = false;
	pQuad->border = true;
	m_curTexture = pQuad->texture;
	m_curTexture->AddRef();
	std::string str = sLocale.GetLocale(45, "Curtex: ");
	str += m_curTexture->GetName();
	m_tText->szText = str;
	sCommUI.SetActiveTexture(m_curTexture->GetName());
}

void TextureUI::BruhshSelProc(RadioButton<TextureUI>* pRButton, bool check)
{
	if(!check)
	{
		pRButton->Check();
		return;
	}
	if(pRButton == m_pCircle)
	{
		m_brushType = 0;
		m_pSquare->Uncheck();
	}
	else if(pRButton == m_pSquare)
	{
		m_brushType = 1;
		m_pCircle->Uncheck();
	}
}

void TextureUI::EnabledTexClick(Checkbox<TextureUI>*, ui32 check)
{
	if(check)
	{
		sSelection.EnableTexturing();
		sChat.AddText("You are in TEXTURING mode!", -1, 0xFFFF0000);
	}
	else
		sChat.AddText("You are in TERRAIN mode!", -1, 0xFFFF0000);
}

void TextureUI::SetTextureFlags(Checkbox<TextureUI>* box, ui32 checked)
{
	if(checked)
	{
		ui32 f = sSelection.GetTexFlags();
		f |= (ui32)box->pData;
		sSelection.SetTexFlags(f);

		Vertex3F pos = sCamera.GetCamPosition();
		ui32 indexX = (ui32)floor(pos.z / TILESIZE);
		ui32 indexY = (ui32)floor(pos.x / TILESIZE);
		ADTModel* mod = sD3D.GetADT(indexX, indexY);
		if(!mod)
			return;
		float rx = pos.x - (indexX * TILESIZE);
		float ry = pos.z - (indexY * TILESIZE);
		indexX = (ui32)floor(rx / CHUNKSIZE);
		indexY = (ui32)floor(ry / CHUNKSIZE);
		ADTChunk* c = mod->GetChunk(indexX, indexY);
		if(!c)
			return;

		if(box == pL1)
		{
			if(m_pCurQuad)
			{
				m_dialog->RemoveQuad(m_pCurQuad);
				m_pCurQuad = 0;
			}
			if(c->GetHeader().nLayers)
			{
				m_pCurQuad = m_dialog->AddTexQuad(this, &TextureUI::TexQuadProc, c->GetTextureName(0), Vertex2F(275, 175), 50);
				m_pCurQuad->visible = true;
			}
			pL2->SetChecked(false);
			pL3->SetChecked(false);
			pL4->SetChecked(false);
		}
		else if(box == pL2)
		{
			if(m_pCurQuad)
			{
				m_dialog->RemoveQuad(m_pCurQuad);
				m_pCurQuad = 0;
			}
			if(c->GetHeader().nLayers >= 2)
			{
				m_pCurQuad = m_dialog->AddTexQuad(this, &TextureUI::TexQuadProc, c->GetTextureName(1), Vertex2F(275, 175), 50);
				m_pCurQuad->visible = true;
			}
			pL1->SetChecked(false);
			pL3->SetChecked(false);
			pL4->SetChecked(false);
		}
		else if(box == pL3)
		{
			if(m_pCurQuad)
			{
				m_dialog->RemoveQuad(m_pCurQuad);
				m_pCurQuad = 0;
			}
			if(c->GetHeader().nLayers >= 3)
			{
				m_pCurQuad = m_dialog->AddTexQuad(this, &TextureUI::TexQuadProc, c->GetTextureName(2), Vertex2F(275, 175), 50);
				m_pCurQuad->visible = true;
			}
			pL1->SetChecked(false);
			pL2->SetChecked(false);
			pL4->SetChecked(false);
		}
		else if(box == pL4)
		{
			if(m_pCurQuad)
			{
				m_dialog->RemoveQuad(m_pCurQuad);
				m_pCurQuad = 0;
			}
			if(c->GetHeader().nLayers >= 4)
			{
				m_pCurQuad = m_dialog->AddTexQuad(this, &TextureUI::TexQuadProc, c->GetTextureName(3), Vertex2F(275, 175), 50);
				m_pCurQuad->visible = true;
			}
			pL1->SetChecked(false);
			pL2->SetChecked(false);
			pL3->SetChecked(false);
		}
	}
	else
	{
		if(box == pL1 || box == pL2 || box == pL3 || box == pL4)
		{
			if(m_pCurQuad)
			{
				m_dialog->RemoveQuad(m_pCurQuad);
				m_pCurQuad = 0;
			}
		}
		ui32 f = sSelection.GetTexFlags();
		f &= ~ui32(box->pData);
		sSelection.SetTexFlags(f);
	}
}